package com.zbec.mdrs.web;

import com.zbec.framework.core.vo.JqgridResponse;
import com.zbec.framework.core.vo.PageInfo;
import com.zbec.framework.security.entity.SysOrganization;
import com.zbec.framework.security.service.SecurityService;
import com.zbec.framework.security.vo.OrganizationVO;
import com.zbec.mdrs.constants.Constants;
import com.zbec.mdrs.entity.ColModelConfig;
import com.zbec.mdrs.entity.guarantee.Guarantee;
import com.zbec.mdrs.service.ColModelConfigService;
import com.zbec.mdrs.service.GuaranteeService;
import com.zbec.mdrs.vo.GuaranteeVo;
import org.json.simple.JSONObject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by fxl on 2015/10/24.
 */
@Controller
@RequestMapping(value = "/guarantee")
public class GuaranteeController extends BaseController {

    @Autowired
    private GuaranteeService guaranteeService;

    @Autowired
    private SecurityService securityService;

    @Autowired
    private ColModelConfigService colModelConfigService;


    @RequestMapping(value = "/toGuarantee")
    public ModelAndView toGuarantee(HttpServletRequest request) {
        ModelAndView view=new ModelAndView();
        OrganizationVO orgVo= securityService.getCurrentUserVO().getBranch();
        if(orgVo!=null && orgVo.getOrgType().equals(Constants.BANK_TYPE_FH)){
            List<OrganizationVO> voList=securityService.findByParentOrgId(orgVo.getId());
            if(voList==null){
                voList=new ArrayList<OrganizationVO>();
            }
            voList.add(0,orgVo);
            view.addObject("orgList",voList);
            view.addObject("isFh","true");
        }else {
            view.addObject("isFh","false");
            if(orgVo!=null){
                view.addObject("orgId",orgVo.getId());
            }
        }
        if(this.isUserReseted(securityService.getCurrentUserVO())){
            view.setViewName("system/editPassword");
        }else{
            view.setViewName("guarantee/guarantee_list");
            String loginName=securityService.getCurrentUser().getLoginName();
            ColModelConfig config= colModelConfigService.findColModelConfig(loginName, Constants.BUSINESS_TYPE_GUARANTEE);
            view.addObject("config",config);
        }
        return view;
    }

    @RequestMapping(value = "/guaranteeList")
    @ResponseBody
    public JqgridResponse<GuaranteeVo> findAllDrafts(GuaranteeVo vo,PageInfo pageInfo,HttpServletRequest request,@RequestParam(value = "_search", defaultValue = "false") String isSearch) {
        vo.setSearch(isSearch);
        return guaranteeService.findAllGuarantee(vo, pageInfo);
    }

    @RequestMapping(value = "/toAddGuarantee")
    public String toAddGuarantee(HttpServletRequest request) {
        return "guarantee/add_guarantee";
    }

    @RequestMapping(value = "/addGuarantee")
    public void addGuarantee(Guarantee guarantee){
        guaranteeService.addGuarantee(guarantee);
    }


    @RequestMapping(value = "/editGuarantee")
    public void editGuarantee(GuaranteeVo guaranteeVo,HttpServletRequest request){
        String userIp=this.getIPAddress(request);
        guaranteeService.updateGuarantee(guaranteeVo,userIp);
    }

    @RequestMapping(value = "/ignoreGuarantee")
    @ResponseBody
    public String ignoreGuarantee(@RequestParam(value = "ids", defaultValue = "") String ids){

        guaranteeService.ignoreGuarantee(ids);
        JSONObject object=new JSONObject();
        object.put("result","true");
        return object.toJSONString();
    }

    @RequestMapping(value = "/toImportGuarantee")
    public String toImportGuarantee(HttpServletRequest request) {
        return "guarantee/guarantee_importUpdate";
    }
}
